Imports System.Reflection
Imports System.ComponentModel

Public Class Common
    Private Shared m_dicEnumDescriptionCache As New Collections.Specialized.StringDictionary

    Public Shared Function GetUniqueID(ByRef Context As HttpContext) As String
        Dim iUniqueId As Integer = 0
        If Context.Items("UniqueId") IsNot Nothing Then
            iUniqueId = DirectCast(Context.Items("UniqueId"), Integer)
            iUniqueId += 1
        End If

        Context.Items("UniqueId") = iUniqueId

        Return String.Format("C{0:x}", iUniqueId)
    End Function

    Public Shared Function ServicePath(ByVal context As HttpContext) As String
        Dim sPath As String = context.Request.CurrentExecutionFilePath
        Return sPath.Substring(0, sPath.LastIndexOf("/"))
    End Function

    Public Shared Function ContainsReference(ByVal ScriptManager As ScriptManager, ByVal Name As String) As Boolean
        For Each objScript As ScriptReference In ScriptManager.Scripts
            If objScript.Name = Name Then Return True
        Next
        Return False
    End Function

    Public Shared Function GetEnumDescription(ByVal Value As Object) As String
        Dim sRetVal As Text.StringBuilder = New Text.StringBuilder()

        Dim objEnumType As Type = Value.GetType
        Dim sKey As String = String.Format("{0}#{1}", objEnumType.ToString, Value.ToString)

        With m_dicEnumDescriptionCache
            'Check if value is already cached (this avoids expensivereflection calls)
            If .ContainsKey(sKey) Then
                'Retrieve from cache
                sRetVal.Append(.Item(sKey))
            Else
                If objEnumType.IsDefined(GetType(FlagsAttribute), False) Then
                    Dim arrValues As Array = [Enum].GetValues(objEnumType)
                    Dim bFirst As Boolean = True

                    Dim iValue As Integer = CInt(Value)

                    For i As Integer = 0 To arrValues.Length - 1
                        If (iValue And arrValues(i)) = arrValues(i) Then
                            If Not bFirst Then
                                sRetVal.Append(", ")
                            End If


                            sRetVal.Append(InternalGetEnumDescription(arrValues(i)))

                            bFirst = False
                        End If
                    Next
                Else
                    sRetVal.Append(InternalGetEnumDescription(Value))

                    'Add to cache for next time
                    .Add(sKey, sRetVal.ToString())
                End If
            End If
        End With

        Return Trim(sRetVal.ToString())
    End Function

    Private Shared Function InternalGetEnumDescription(ByVal Value As Object) As String
        Dim sRetVal As String
        Dim objEnumType As Type = Value.GetType
        Dim objFieldInfo As FieldInfo = objEnumType.GetField([Enum].GetName(objEnumType, Value))

        Dim objDescriptionAttributes() As DescriptionAttribute = objFieldInfo.GetCustomAttributes(GetType(DescriptionAttribute), False)
        With objDescriptionAttributes
            If .Length > 0 Then
                sRetVal = objDescriptionAttributes(0).Description
            Else
                'Still not found, just use the enum value
                sRetVal = [Enum].GetName(objEnumType, Value)
            End If
        End With

        Return sRetVal
    End Function
End Class

Public Class Result
    Public Success As Boolean
    Public ErrorMessage As String
End Class

Public Class Request
    Public SID As String
End Class
